#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include <unordered_map>

#include "include/mysql.h"
#include "../comm/util.hpp"
#include "../comm/log.hpp"

/**************************************
 * MYSQL版本
 * 根据题目list文件，将所有题目加载到内存
 * model: 数据交互模块, 对外提供访问数据的接口,
 */
namespace ns_model
{
    using namespace ns_util;
    using namespace ns_log;

    struct Question
    {
        std::string id;         // 题目编号
        std::string title;      // 题目标题
        std::string star;       // 题目难度
        int cpu_limit;          // 时间限制
        int mem_limit;          // 空间限制
        std::string desc;       // 题目描述
        std::string header;     // 题目预设代码
        std::string tail;       // 题目测试用例, 与header链接形成最终代码
    };
    
    const std::string table_name = "oj_questions";
    const std::string host = "127.0.0.1";
    const std::string user = "oj_client";
    const std::string password = "123456";
    const std::string db = "oj";
    const int port = 3306;


    class Model
    {
    public:
        Model() = default;
        ~Model() = default;
    public:
        bool QueryMysql(const std::string sql, std::vector<Question>* out)
        {
            // mysql句柄
            MYSQL* mysql = mysql_init(nullptr);
            // MYSQL my;
            // MYSQL* mysql = mysql_init(&my);

            // 连接数据库
            if(nullptr == mysql_real_connect(mysql, host.c_str(), user.c_str(), password.c_str(),\
                 db.c_str(), port, nullptr, 0))
            {
		        // std::cout << mysql_error(&my) << std::endl;
                LOG(FATAL) << "连接数据库失败!\n";
                return false;
            }

            // 设置编码格式
            mysql_set_character_set(mysql, "utf8");
            LOG(INFO) << "连接数据库成功!\n";

            // 执行sql语句
            if(mysql_query(mysql, sql.c_str()) != 0)
            {
                LOG(WARNING) << sql << " execute error!\n";
                return false;
            }

            // 提取结果
            MYSQL_RES* res = mysql_store_result(mysql);

            // 分析结果
            int rows = mysql_num_rows(res);
            int cols = mysql_num_fields(res);
            Question question;
            for(int i = 0; i < rows; ++i)
            {
                MYSQL_ROW row = mysql_fetch_row(res);
                question.id = row[0];
                question.title = row[1];
                question.star = row[2];
                question.desc = row[3];
                question.header = row[4];
                question.tail = row[5];
                question.cpu_limit = atoi(row[6]);
                question.mem_limit = atoi(row[7]);

                out->push_back(question);
            }

            free(res);      // 释放空间
            mysql_close(mysql);     // 关闭连接 
            return true;
        }

        bool GetAllQuestions(std::vector<Question>* out)
        {
            std::string sql = "select * from " + table_name;
            return QueryMysql(sql, out);
        }

        bool GetOneQuestion(Question* out, const std::string& id)
        {
            bool ret = false;
            std::string sql = "select * from " + table_name;
            sql += " where id=";
            sql += id;
            std::vector<Question> result;
            if(QueryMysql(sql, &result))
            {
                if(result.size() == 1)
                {
                    *out = result[0];
                    ret = true;
                }
            }
            return ret;
        }
    };
} 
